//
// @author 风杨
// @date 22-9-2 上午11:19
// @see <a href="yueny09@163.com">mailTo: yueny09@163.com</a>
//

use std::fmt::format;
use std::time::Instant;
// use crate::duration::SystemTime;
//
// struct SystemTime();

/// 耗时统计的结构体对象
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DurationX {
    now: Instant,

}

/// 耗时统计
///
/// # Examples
///
/// ```
/// use std::{thread, time};
/// use whosly::duration::DurationX;
///
/// let now = DurationX::new();
///
/// let ten_millis = time::Duration::from_millis(100);
/// thread::sleep(ten_millis);
///
/// let duration = now.release();
/// println!("耗时：{}", duration);
/// ```
impl DurationX{
    /// 实例化
    pub fn new() -> DurationX {
        DurationX{
            now : Instant::now()
        }
    }

    /// 结束统计卯点
    pub fn release(&self) -> String {
        let duration = self.now.elapsed();
        let s= duration.as_secs();
        let mut ms=duration.as_millis();
        let mut us=duration.as_micros();

        us-=ms*1000;
        ms-=s as u128*1000;

        if s == 0 {
            if ms == 0 {
                format!("{} us", us)
            }else{
                format!("{} ms {} us", ms, us)
            }
        }else{
            format!("{} s {} ms {} us", s, ms, us)
        }
    }

}